<?php 
class pageHTML {
    
    static $total;
    
    static $HTMLpage;
    
    static $getp='p';
    
    /**
     * 
     *  取得$sql的总记录数
    * @param string $sql sql语句
     */
    static function total($sql){
		$webdb=R::getClass('mdb');
        $query=$webdb->query($sql);
        $count=$webdb->num_rows($query);
        self::$total=$count;
        return $count;
    }
    
    /**
     * 
     * 根据当前页的页码数，算出sql语句后面的limit数
     * @param string $sql sql语句
     */
    static function pageLimt($sql){
	    $pageNum=getConfig('pageNum');
	    $requests=R::getClass('requests');
		$getp=isset($requests->get[self::$getp])?$requests->get[self::$getp]:0;
		$page=isset($getp)?$getp:0;
		$getp<=0 && $page=0;	
		$page=$page==0?0:$page-1;
		$fpage=$page * $pageNum;
		self::total($sql);		
		$limit=' LIMIT '.$fpage.','.$pageNum;

		return $limit;        
    }
    
    /**
     * 
     * 得到分页的HTML
     * @param array  $language
     */
    static function getHtml($language=''){
		$config=R::getClass('config');
        if(self::$total>0){          		
					
			$pagenum=$config->pageNum;
			$pageClass=new page(array('total'=>self::$total,'perpage'=>$pagenum,'url'=>self::get_url(),'pagebarnum'=>$config->pagebarnum));
			
			
	        $pageClass->next_page=$language['page_next'];
	        
	        $pageClass->pre_page=$language['page_pre'];
	        
	        $pageClass->first_page=$language['page_first'];
	        
	        $pageClass->last_page=$language['page_last'];
	        
	        $pageClass->page_txt=$language['page_'];
			
			$pageClass->page_name=self::$getp;
			
			self::$HTMLpage=$pageClass->show($config->page_style);

        }else{
            self::$HTMLpage='';
        }
        return self::$HTMLpage;
    }
    
    private static function get_url(){
    	$url=R::exurl();
    	$do=implode('/',$url);
    	$php_self=basename($_SERVER['PHP_SELF'],'.php');
        return get_url($do);
    }
}